<?php
/*------------------------------------------------------------------------------ 
    
    Create Date :2010-08-13
    Author    : Phan Van Dam
    Copyright(c) 2010 A.D.A Solution. All rights reserved
    
    ------------------------------------------------------------------------------
    Update History:
    Ver.    TRB#            Date          Author       Note
    3.0     thiếu comment  2011/03/10     HoangPhuc     Review code,update comment for source
------------------------------------------------------------------------------*/

if(!defined('IN_ADAGPS'))
{
    die(SYSTEM_ACCESS_DENIED);
}
$objXajax->registerFunction('frmproduct_loadGrid');      
$objXajax->registerFunction('frmproduct_save');
$objXajax->registerFunction('frmproduct_edit');
$objXajax->registerFunction('frmproduct_delete');
$objXajax->registerFunction('frmproduct_fillMenuParent');

function frmproduct_save($params)
{
    global $objDbSelect;
    $objResponse = new xajaxResponse();
    $hotel_id = $_SESSION['current']['hotel_id'];   
    $user_id = $_SESSION['current']['user_id'];        
    $name_vi = $params['txtnamemenuVi'];
    $name_en = $params['txtnamemenuEn'];
    $parent_id = $params['cbbMenuParent'];
    $price = $params['txtpricemenu'];
    $unit = $params['txtunitmenu'];
    $unitname = $params['txtnameunit'];
    $edit = $params['hdEdit'];
    $id = $params['hdId'];

//    echo '<pre>';
//    print_r($params);
//    die();
    $sql = ""; 
    if($edit==1){
        $sql .="UPDATE tbl_product_new SET name_vi = '$name_vi',name_en = '$name_en',parent_id = '$parent_id',
            price = '$price',unit = '$unit',nameunit='$unitname',user_update = '$user_id',date_update = now() WHERE id =$id"; 
      // echo $sql;die();
        $objDbSelect->Execute($sql);
        
    }else{
          $sql.= " INSERT INTO tbl_product_new(name_vi,name_en,active,unit,nameunit,price,parent_id,user_created,
          date_created,user_update,date_update) VALUES('$name_vi','$name_en',1,'$unit',
          '$unitname','$price','$parent_id','$user_id',now(),'$user_id',now())"; 
          //echo $sql; die();
          $objDbSelect->Execute($sql); 
     }    
    $objResponse->addScript('changeWaiting(1);');
    return $objResponse->getXML();
}
function frmproduct_fillMenuParent()
{
    global $objDbSelect,$lang;
    $objResponse = new xajaxResponse();
    $hotel_id = $_SESSION['current']['hotel_id'];
    $sql = " SELECT id, name_$lang as name FROM tbl_product_new_type WHERE 
       ( menu_type = 1 or  id not in (SELECT distinct parent_id from tbl_product_new_type WHERE id <> parent_id ) ) and hotel_id = '$hotel_id'";    
    $arr = $objDbSelect->GetArray($sql);
    $count = count($arr);
    $temp ='';
    for($i=0;$i<$count;$i++){
        $id = $arr[$i]['id'];
        $name = $arr[$i]['name'];
        $temp.= "<option value='$id'> $name </option>"  ;
    }
    $temp =  '<select id="frmproduct_cbbMenuParent" class="cbbRequired"><option value="">--Chọn--</option>'.$temp.'</select>';
    $objResponse->addAssign('frmproduct_tdmenuparent', 'innerHTML',$temp);
    return $objResponse->getXML();
}
function frmproduct_loadGrid()
{     

    global $lang,$path;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $lang ='vi';
    $arrGrid = frmproduct_getGrid();    
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('../templates/'.$lang.'/'.$path.'/hotel_productnew_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('frmproduct_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTableGroup('frmproduct_tbl');");                              
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}
function frmproduct_getGrid()
{
    global $objDbSelect,$lang;
    $hotel_id = $_SESSION['current']['hotel_id'];
    $sqlSelect = " select pt1.id,pt1.active, pt1.name_vi,pt1.name_en,pt2.name_$lang as parent_name,pt1.price as price, 
                   pt1.unit as unit,pt1.nameunit as nameunit,pt1.icon as icon from tbl_product_new as pt1 INNER JOIN  tbl_product_new_type as pt2 ON 
                   pt1.parent_id = pt2.id WHERE pt2.hotel_id = '$hotel_id' order by pt1.parent_id,pt1.name_$lang ";
                   
                 // echo $sqlSelect; die();
                   
    return $objDbSelect->GetArray($sqlSelect);
}  

    function frmproduct_edit($id){
        global $objDbSelect;
        $objResponse = new xajaxResponse();
        $sql = " SELECT * FROM tbl_product_new WHERE id = '$id' ";
        $arr = $objDbSelect->GetArray($sql);
        $name_vi        = $arr[0]['name_vi'];
        $name_en   = $arr[0]['name_en'];    
        $parent_id     = $arr[0]['parent_id'];
        $unit         = $arr[0]['unit'];
        $nameunit         = $arr[0]['nameunit'];
        $price         = $arr[0]['price'];       
        
        $objResponse->addScript("$('#frmproduct_txtnamemenuVi').val('$name_vi');");
        $objResponse->addScript("$('#frmproduct_txtnamemenuEn').val('$name_en');");
        $objResponse->addScript("$('#frmproduct_cbbMenuParent').val('$parent_id');");
        $objResponse->addScript("$('#frmproduct_txtpricemenu').val('$price');");
        $objResponse->addScript("$('#frmproduct_txtunitmenu').val('$unit');");
        $objResponse->addScript("$('#frmproduct_txtnameunit').val('$nameunit');");
        $objResponse->addScript('changeWaiting(1);');
        return $objResponse->getXML();   
    }
    
?>
